package com.aaa.custom.dao.orderDao;



import com.aaa.entity.cart_order.zxh.Order;
import com.aaa.entity.cart_order.zxh.Order_Item;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * fileName:OrderDao
 * description:
 * author:zxh
 * createTime:2020/1/12 22:09
 * version:1.0.0
 */
public interface OrderDao {
    /**
     * 单个订单查询
     * @param order_sn
     * @return
     */
    @Select("select order_id,sp1,order_sn,product_pic,product_name,product_price," +
            "product_id,product_quantity,coupon_amount," +
            "integration_amount,real_amount from oms_order_item" +
            " where order_sn=#{order_sn}")
    List<Order_Item> byOrder_snGetOrder(String order_sn);

    /**
     * 全部订单查询表
     * @return
     */
    @Select("<script>" +
            "select id,member_id," +
            "order_sn ,shop_id,create_time,status," +
            "total_amount,pay_amount,order_type ," +
            "auto_confirm_day,bill_receiver_phone ,bill_receiver_email " +
            ",receiver_name ,receiver_post_code ,receiver_province ,receiver_city " +
            ", receiver_region ,receiver_detail_address,note,confirm_status,delete_status," +
            "payment_time ,delivery_time ,order_status from oms_order " +
            "where delete_status=0 "+
            "<if test='id!=null and id!=0'> " +
            "and id =#{id}" +
            "</if>" +
            "<if test='member_id!=null and member_id!=0'> " +
            "and member_id =#{member_id}" +
            "</if>" +
            "<if test='shop_id!=null and shop_id!=0'> " +
            " and shop_id =#{shop_id}" +
            "</if>" +
            "<if test=\"order_sn!=null and order_sn!=''\"> " +
            " and order_sn =#{order_sn}" +
            "</if>" +
            "</script> ")
    @Results({
            @Result(property = "order_items",column = "order_sn",
                    many = @Many(select = "com.aaa.custom.dao.orderDao.OrderDao.byOrder_snGetOrder")
            )
    })
    List<Order> byMember_idAndShop_idGetShop(@Param("id") Long id,@Param("member_id") Long member_id,@Param("shop_id") Long shop_id,@Param("order_sn") String order_sn);

    /**
     * 删除订单（将订单状态改为删除）||修改订单状态
     * delete_status订单是否删除，confirm_status确认收货状态，status订单状态
     * @param id  payment_time
     * @return
     */
    @Update("<script>" +
            "update oms_order " +
            "<set> " +
            "<if test=\"delete_status!=null and delete_status!=''\">" +
            "delete_status=#{delete_status}," +
            "</if>" +
            "<if test=\"status==1 \">" +
            "payment_time=now()," +
            "</if>" +
            "<if test=\"confirm_status!=null and confirm_status!=''\">" +
            "confirm_status=#{confirm_status}," +
            "</if>" +
            "<if test=\"status!=null and status!='' \">" +
            "status=#{status}," +
            "</if>" +
            "</set>" +
            "where id=#{id}" +
            "</script>")
     int delectOrderByID(@Param("id") Long id,@Param("delete_status")Integer delete_status,@Param("confirm_status")Integer confirm_status,@Param("status")Integer status );

    /**
     * 添加订单表
     * @param map
     * @return
     */
    @Insert("insert into " +
            "oms_order( " +
            "member_id,order_sn,shop_id,create_time,total_amount,pay_amount,status" +
            "order_type ,auto_confirm_day,bill_receiver_phone,bill_receiver_email," +
            "receiver_name ,receiver_post_code ,receiver_province ,receiver_city ," +
            "receiver_region ,receiver_detail_address," +
            "note,confirm_status,delete_status,delivery_time ,order_status)" +
            "VALUES(#{member_id},#{order_sn},#{shop_id},now()," +
            "#{total_amount},#{pay_amount},#{stauts},#{order_type} ,#{auto_confirm_day}," +
            "{bill_receiver_phone},#{bill_receiver_email},#{receiver_name} ,#{receiver_post_code}" +
            " ,#{receiver_province} ,#{receiver_city} ,#{receiver_region},#{receiver_detail_address}," +
            "#{note},#{confirm_status}," +
            "#{delete_status} ,#{delivery_time} ,#{order_status})")
    int add_oms_order(Map map);

    /**
     * 添加商品订单中间表
     * @param map
     * @return
     */
    @Insert("<script>" +
            "insert into  " +
            "oms_order(order_id,order_sn,product_pic,product_name,product_price," +
            "product_id,product_quantity,coupon_amount,integration_amount,real_amount) " +
            "VALUES " +
            "<foreach collection='list' item='orderProduct' separtor=','>" +
            "(#{orderProduct.order_id},#{orderProduct.order_sn},#{orderProduct.product_pic},#{orderProduct.product_name},#{orderProduct.product_price},#{orderProduct.product_id}," +
            "#{orderProduct.product_quantity},#{orderProduct.coupon_amount},#{orderProduct.integration_amount},#{orderProduct.real_amount})" +
            "</foreach>" +
            "</script>")
    int add_oms_order_item(List<Map> orderProducts);
}
